/*
 * Copyright 2010 Biz-e (info@biz-e.nl)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.biz_e.appengine.gar.shared;

import nl.biz_e.appengine.gar.client.i18n.Constants;

/**
 * A Role describes what a specific {@link User} can do in the system,
 * and so provides a way of authorization.
 * 
 * @author Joost A. Bloemsma
 */
public class Role {
    private static enum RoleType {
        PARENT,
        CHILDMINDER,
        BURO,
        ADMINISTRATOR
    }

    /** Enumerated Role representing the Parent role */
    public static Role parent = new Role(RoleType.PARENT);
    /** Enumerated Role representing the Childminder role */
    public static Role childMinder = new Role(RoleType.CHILDMINDER);
    /** Enumerated Role representing the Buro role */
    public static Role buro = new Role(RoleType.BURO);
    /** Enumerated Role representing the Administrator role */
    public static Role admin = new Role(RoleType.ADMINISTRATOR);

    private RoleType type;

    private Role() {
        // Just to make sure this default constructor cannot be used
    }

    private Role(RoleType type) {
        this.type = type;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String toString() {
        switch (this.type) {
            case PARENT:
                return Constants.getGarConstants().parent();
            case CHILDMINDER:
                return Constants.getGarConstants().childminder();
            case BURO:
                return Constants.getGarConstants().buro();
            case ADMINISTRATOR:
                return "administrator";
            default:
                return "UNKNOWN";
        }

    }

    /**
     * @return The name of the enumerated type
     */
    String getRoleEnumName() {
        return type.name();
    }

    /**
     * Returns the role that has the type corresponding to the passed name
     * 
     * @param enumName
     *            Name of enumerated role type
     * @return the role
     */
    public static Role getRoleByEnumName(String enumName) {
        RoleType type = RoleType.valueOf(enumName.toUpperCase());
        return getRoleByEnumType(type);
    }

    /**
     * Returns the role that has the passed type
     * 
     * @param type
     *            The enumerated type
     * @return The role
     */
    public static Role getRoleByEnumType(RoleType type) {
        switch (type) {
            case PARENT:
                return parent;
            case CHILDMINDER:
                return childMinder;
            case BURO:
                return buro;
            case ADMINISTRATOR:
                return admin;
            default:
                return null;
        }
    }
}
